<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Cache;
use DB;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //人群缓存
        if(!Cache::has('crowds'))
        {
            $crowds=DB::table('crowd')->get();
            Cache::forever('crowds',$crowds);
        }
        $crowds=Cache::get('crowds');
        //运动缓存
        if(!Cache::has('sports'))
        {
            $sports=DB::table('sport')->get();
            Cache::forever('sports',$sports);
        }
        $sports=Cache::get('sports');
        //品牌缓存
        if(!Cache::has('allbrands'))
        {
            $brands=DB::table('brand')->get();
            Cache::put('allbrands',$brands,2);
        }
        $allbrands=Cache::get('allbrands');
        //男子包含运动缓存
        if(!Cache::has('msports'))
        {
            $msports_id=DB::table('cro_spo_rel')->where('cro_id','=','1')->lists('spo_id');
            $msports=DB::table('sport')->whereIn('sport_id',$msports_id)->select('sport_id','sport_type')->get();
            Cache::forever('msports',$msports);
        }
        $msports=Cache::get('msports');
        //女子包含运动缓存
        if(!Cache::has('wsports'))
        {
            $wsports_id=DB::table('cro_spo_rel')->where('cro_id','=','2')->lists('spo_id');
            $wsports=DB::table('sport')->whereIn('sport_id',$wsports_id)->select('sport_id','sport_type')->get();
            Cache::forever('wsports',$wsports);
        }
        $wsports=Cache::get('wsports');
        //儿童包含运动缓存
        if(!Cache::has('csports'))
        {
            $csports_id=DB::table('cro_spo_rel')->where('cro_id','=','3')->lists('spo_id');
            $csports=DB::table('sport')->whereIn('sport_id',$csports_id)->select('sport_id','sport_type')->get();
            Cache::forever('csports',$csports);
        }
        $csports=Cache::get('csports');

        if(!Cache::has('frilinks_pic'))
        {
            $frilinks_pic=DB::table('fri_link')->where('disabled','=','1')->where('pic','<>','')->take(6)->get();
            Cache::put('frilinks_pic',$frilinks_pic,1);

        }
        $frilinks_pic=Cache::get('frilinks_pic');
        if(!Cache::has('firlinks_nopic'))
        {
            $frilinks_nopic=DB::table('fri_link')->where('disabled','=','1')->where('pic','=','')->take(12)->get();
            Cache::put('frilinks_nopic',$frilinks_nopic,1);
        }
        $frilinks_nopic=Cache::get('frilinks_nopic');

        view()->share('crowds',$crowds);
        view()->share('msports',$msports);
        view()->share('wsports',$wsports);
        view()->share('csports',$csports);
        view()->share('allbrands',$allbrands);
        view()->share('frilinks_pic',$frilinks_pic);
        view()->share('frilinks_nopic',$frilinks_nopic);
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}
